<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px"
    >
      <el-form-item label="应用信息" prop="app_id">
        <el-select
          v-model="queryParams.app_id"
          placeholder="请选择应用信息"
        >
          <el-option
            v-for="item in appOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.customer_activity_record_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.customer_activity_record_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会员名称" prop="customer_name">
        <el-input
          v-model="queryParams.customer_name"
          placeholder="请输入会员名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精确会员名称" prop="customer_exact_name">
        <el-input
          v-model="queryParams.customer_exact_name"
          placeholder="请输入精确会员名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="customer_phone">
        <el-input
          v-model="queryParams.customer_phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker
          v-model="registerTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="createTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" icon="el-icon-search" size="mini"
          v-hasPermi="['customer:activityRecord:query']" @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="showClose">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @sort-change="sortChange">
      <el-table-column sortable="custom" label="记录编号" align="center" key="id" prop="id"/>
      <el-table-column label="会员id" align="center" key="customer_id" prop="customer_id"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column label="会员名称" align="center" key="name" prop="name"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column label="会员手机号" align="center" key="phone" prop="phone"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column label="一级代理" align="center" key="first_agent_name" prop="first_agent_name"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column label="注册时间" align="center" key="register_time" prop="register_time" width="160"/>
      <el-table-column label="创建时间" align="center" key="create_time" prop="create_time" width="160"/>
      <el-table-column label="活动id" align="center" key="activity_id" prop="activity_id"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column prop="type" label="类型" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_activity_record_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_activity_record_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {getGroupApp} from "@/api/metadata/app";
import {listCustomerActivityRecord} from "@/api/customer/customerActivityRecord";

export default {
  name: 'CustomerActivityRecord',
  computed: {},
  dicts: ['customer_activity_record_status', 'customer_activity_record_type'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: null,
      // 弹出层标题
      title: '',
      // 弹出层标题
      resetTitle: '',
      resetTradeTitle: '',
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      resetPwdOpen: false,
      resetTradePwdOpen: false,
      admin: false,
      // 日期范围
      registerTimeRange: [],
      createTimeRange: [],
      appOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        app_id: undefined,
        customer_name: undefined,
        customer_exact_name: undefined,
        customer_phone: undefined,
        customer_email: undefined,
        status: undefined,
        type: undefined,
        sort_list: [],
      },
      // 表单校验
      rules: {},
      showClose: false,
    }
  },
  created() {
    const exactName = this.$route.params && this.$route.params.exactName;
    if (exactName) {
      this.queryParams.customer_exact_name = exactName
      this.showClose = true
    }
    // 默认id排序
    this.queryParams.sort_list = [{sort_by: 'id', sort_type: 2}]
    const appId = this.$route.params && this.$route.params.appId;
    this.getGroupApp(appId)
  },
  methods: {
    sortChange(column) {
      if (column) {
        if (column.order === 'ascending') {
          this.queryParams.sort_list = [{sort_by: column.prop, sort_type: 1}]
        } else if (column.order === 'descending') {
          this.queryParams.sort_list = [{sort_by: column.prop, sort_type: 2}]
        }
        this.getList()
      }
    },
    getList() {
      this.loading = true
      listCustomerActivityRecord(
        this.addDateRange(
          this.addDateRange(this.queryParams, this.registerTimeRange, '_register_time'),
          this.createTimeRange, '_create_time'
        )
      ).then(response => {
          this.dataList = response.data
          this.total = response.total
          this.loading = false
        }
      )
    },
    getGroupApp(appId) {
      getGroupApp().then(response => {
        this.queryParams.type = 1
        this.appOptions = response.data
        if (appId) {
          this.queryParams.app_id = parseInt(appId)
        } else {
          this.queryParams.app_id = response.data[0].id
        }
        this.getList()
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.resetPwdOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.registerTimeRange = []
      this.createTimeRange = []
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.handleQuery()
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = {path: "/customer/customerUser"};
      this.$tab.closeOpenPage(obj);
    },
  }
}
</script>
